<template>
    <el-page-header icon="el-icon-arrow-left" @back="$router.replace('/employee')" content="员工管理"></el-page-header>
    <el-divider></el-divider>
    <el-form :model="formData" ref="myForm" :rules="rules" label-width="100px">
        <el-form-item v-if="isUpdate" label="员工编号" prop="empNumber">
            <el-input v-model="formData.empNumber" readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username" placeholder="如: zhangsan"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="empName">
            <el-input v-model="formData.empName" placeholder="如: 张三"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="idcard">
            <el-input v-model="formData.idcard" placeholder="如: 441802000000000000"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
            <el-select v-model="formData.roleId" filterable placeholder="请选择">
                <el-option label="请选择" :value="-1"></el-option>
                <el-option
                        v-for="item in roleOptions"
                        :key="item.id"
                        :label="item.roleDesc"
                        :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('myForm')">提交</el-button>
            <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>

    import {ElMessage} from 'element-plus'

    export default {
        data() {
            return {
                isUpdate: false,
                formData: {
                    id: '',
                    username: '',
                    empNumber: '',
                    empName: '',
                    idcard: '',
                    roleId: -1
                },
                roleOptions: [],
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    empName: [
                        {required: true, message: '请输入员工名称', trigger: 'blur'}
                    ],
                    idcard: [
                        {required: true, message: '请输入身份证', trigger: 'blur'}
                    ],
                    roleId: [
                        {required: true, min: "1", type: 'number', message: '请选择角色', trigger: 'blur'},
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (!valid) return false;
                    // 发送网络请求
                    if (this.isUpdate) {    // 更新
                        this.axios.put('employee', this.formData)
                            .then(resp => {
                                if (!resp.data.flag) {
                                    ElMessage.error(resp.data.msg);
                                    return;
                                }
                                ElMessage.success(resp.data.msg);
                                this.$router.replace('/employee');
                            });
                    } else {    // 新增
                        this.axios.post('employee', this.formData)
                            .then(resp => {
                                if (!resp.data.flag) {
                                    ElMessage.error(resp.data.msg);
                                    return;
                                }
                                ElMessage.success(resp.data.msg);
                                this.$router.replace('/employee');
                            });
                    }
                });
            }
        },
        created() {
            let id = this.$route.query.id;
            if (id > 0) {
                this.isUpdate = true;
                this.axios.get('employee/' + id)
                    .then(resp => {
                        if (!resp.data.flag) {
                            ElMessage.error(resp.data.msg);
                            return;
                        }
                        this.formData = resp.data.data.employee;
                    });
            }
            // 查询角色列表
            this.axios.get('role/findOptions')
                .then(resp => {
                    if (!resp.data.flag) {
                        ElMessage.error(resp.data.msg);
                        return;
                    }
                    this.roleOptions = resp.data.data.list;
                });
        }
    }
</script>

<style scoped>

</style>